package com.esm.sm800.database.manager;

import com.esm.sm800.activity.AppContext;
import com.esm.sm800.database.dbmodel.Needle;
import com.esm.sm800.database.greendao.NeedleDao;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

public class NeedleCenter {

    private static NeedleCenter instance = new NeedleCenter();

    public static NeedleCenter getInstance() {
        return instance;
    }

     public Needle getNeedle(String name, int pos) {
        Needle Needle = null;
        try {
            NeedleDao NeedleDao = AppContext.getInstances().getDaoSession().getNeedleDao();
            ArrayList<Needle> list = new ArrayList<Needle>();
            list = (ArrayList) NeedleDao.queryBuilder()
                    .where(com.esm.sm800.database.greendao.NeedleDao.Properties.Position.eq(pos))
                    .where(com.esm.sm800.database.greendao.NeedleDao.Properties.Name.eq(name))
                    .build().list();
            if (list.size() > 0) {
                Needle = list.get(0);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return Needle;
    }

    public Needle getNeedlebyid(long id) {
        Needle Needle = null;
        try {
            NeedleDao NeedleDao = AppContext.getInstances().getDaoSession().getNeedleDao();
            ArrayList<Needle> list = new ArrayList<Needle>();
            list = (ArrayList) NeedleDao.queryBuilder()
                    .where(com.esm.sm800.database.greendao.NeedleDao.Properties.Id.eq(id))
                    .build().list();
            if (list.size() > 0) {
                Needle = list.get(0);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return Needle;
    }

    public Long saveNeedle(Needle Needle) {
        if (Needle == null) {
            return 0L;
        }

        Needle a = getNeedle(Needle.getName(), Needle.getPosition());
        if (a != null) {
            Needle.setId(a.getId());
        }

        try {
            NeedleDao NeedleDao = AppContext.getInstances().getDaoSession().getNeedleDao();
            long id = NeedleDao.insertOrReplace(Needle);
            return id;
        } catch (Exception e) {
            e.printStackTrace();
        }

        return 0L;
    }

    public Long insertNeedle(Needle Needle) {
        if (Needle == null) {
            return 0L;
        }

        try {
            NeedleDao NeedleDao = AppContext.getInstances().getDaoSession().getNeedleDao();
            long id = NeedleDao.insertOrReplace(Needle);
            return id;
        } catch (Exception e) {
            e.printStackTrace();
        }

        return 0L;
    }

    public ArrayList<Needle> getNeedlelist() {
        ArrayList<Needle> list = new ArrayList<Needle>();
        try {
            NeedleDao NeedleDao = AppContext.getInstances().getDaoSession().getNeedleDao();
            list = (ArrayList) NeedleDao.queryBuilder().build().list();
        } catch (Exception e) {
            e.printStackTrace();
        }

        return list;
    }

    //初始化针组
    public void initZz() {
        ArrayList<Needle> list = new ArrayList<Needle>();
        try {
            //获取当前日期
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date curDate = new Date();
            String strDate = formatter.format(curDate);
            //

            NeedleDao NeedleDao = AppContext.getInstances().getDaoSession().getNeedleDao();
            list = (ArrayList) NeedleDao.queryBuilder().build().list();
            if (list.size() < 1) {
                for (int i = 1; i < 13; i++) {
                    Needle needle = new Needle();
                    needle.setPosition(i);
                    needle.setCreat_time(strDate);
                    needle.setUpdate_time(strDate);
                    needle.setWarning_num(5);
                    NeedleDao.insertOrReplace(needle);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

}
